package com.kingcore.director.search.ui;



import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.mira.lucene.analysis.IK_CAnalyzer;

public class LocalFileAnalyzer extends Analyzer{
	private Analyzer dicAnalyzer = new IK_CAnalyzer();
	 
	public TokenStream tokenStream(String fieldName, Reader reader) {
		StringBuffer temp = new StringBuffer();
		char[] ctemp = new char[1024];
		StringBuffer output = new StringBuffer();
		String input = null;
		
		try {
			while(reader.read(ctemp) != -1) {
				temp.append(ctemp); 
			}
			input = temp.toString().trim();
			
			//按照词库切分
			StringReader readerInput = new StringReader(input);
			TokenStream ts = dicAnalyzer.tokenStream("dummy",readerInput); 
			while(true){
				Token t = ts.next();
				if(t != null){
					output.append(t.termText());
					output.append(" ");
				} else {
					break;
				}
			}
			
			if(input.length() > 1) {
				for(int i = 0; i < input.length(); i++) {
					if(!String.valueOf(input.charAt(i)).equals( DELIMETER)) {
						output.append(input.charAt(i)).append(" ");
					}
				}
			}
			
			reader.close();
			reader = new StringReader(output.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new LocalFileTokenStream(reader);
	}
	/** 字段分隔符 */
	public static String DELIMETER = "#deli";
	public static void outputAnalyzer(Analyzer a ,String input) throws Exception{
		StringReader readerInput = new StringReader(input);
	   TokenStream ts = a.tokenStream("dummy",readerInput);
	   
	   while(true){
	     Token t = ts.next();
	     if(t!=null){
	       System.out.print(t.termText());
	       System.out.print(" ");
	     }
	     else
	     break;

	   }
	 System.out.println(" ");
	 ts.close();
	}
	
	
	public static void main(String args[]) {
		try {
			LocalFileAnalyzer.outputAnalyzer(new LocalFileAnalyzer(), "今天你好吗");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
